package com.op.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.op.constants.NumberConstant;
import com.op.entity.Role;
import com.op.mapper.RoleMapper;
import com.op.service.RoleService;
import com.op.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Role)表服务实现类
 *
 * @author 孤焰
 * @since 2021-10-16 22:09:54
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    RoleMapper roleMapper;
    @Resource
    UserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean logicDelBatchById(List<Long> idList) {
        for (Long id : idList) {
            if (userService.existUserByRoleId(id)) {
                throw new RuntimeException("当前角色存在用户绑定 roleId:" + id);
            }
            
            Role role = new Role();
            role.setId(id);
            role.setStatus(NumberConstant.DISABLE);
            if (roleMapper.updateById(role) <= 0) {
                throw new RuntimeException("部分删除失败");
            }
        }
        return true;
    }
}

